﻿<template>
    <full-screen-dialog
        :title="titleMap[mode]"
        v-model="visible"
        :width="1000"
        destroy-on-close
        :close-on-click-modal="false"
        :before-close="handleBeforeClose"
    >
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="要货时间" prop="applyDate">
                        <el-date-picker
                            v-model="entityObj.applyDate"
                            type="datetime"
                            value-format="YYYY-MM-DD"
                            format="YYYY-MM-DD"
                            placeholder="请输入要货时间"
                            style="width: 100%;"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="orgType==2">
                    <el-form-item label-width="120px" label="供货单位" prop="purchaseUnityId">
                        <sc-purchase-unity-select
                            placeholder="---请选择采购单位---"
                            ref="purchaseUnitySelect"
                            :props="purchaseUnityProps"
                            :selectKeyValue="'purchaseUnityId'"
                            v-model="entityObj.purchaseUnitys"
                            :queryType="`store/${orgId}/purchase-unity-pages`"
                            @fatherMethod="purchaseUnitySelectProcess"
                            :multiple="false"
                        ></sc-purchase-unity-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8" v-if="orgType==1">
                    <el-form-item label-width="120px" label="要货门店" prop="storeId">
                        <sc-store-select
                            ref="storeSelect"
                            :props="storeProps"
                            :selectKeyValue="'storeId'"
                            :multiple="false"
                            :defaultQueryList="{ purchaseUnityId: orgId }"
                            v-model="entityObj.stores"
                            @fatherMethod="storeSelectProcess"
                        ></sc-store-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <!-- <el-upload
                        ref="upload"
                        :auto-upload="false"
                        :on-change="handleFileChange"
                        :file-list="fileList"
                        list-type="text"
                    >
                        <el-button>导入</el-button>
                    </el-upload>
                    <el-link
                        href="https://oss.jtmckj.com/Supply/Excel/applyMB.xlsx"
                        size="small"
                        target="_blank"
                        type="primary"
                        :underline="false"
                    >下载模板</el-link> -->
                    <el-form-item label-width="120px" label="导入附件" prop="applyFile">
                        <sc-upload-file
                            v-model="entityObj.applyFile"
                            :objName="'Supply/ApplyOrder'"
                            :uploadFileVal="fileList"
                            @update:uploadFileVal="updateFileList"
                            draggable
                            :uploadType="'OSS'"
                            :bucketName="'jtmc'"
                            :limit="1"
                            :accept="'.xlsx'" 
                        />   
                        <el-link href="https://oss.jtmckj.com/Supply/Excel/applyMB.xlsx" size="small" target="_blank" type="primary" :underline="false" >下载模板</el-link>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <template #footer>
            <el-button @click="handleBeforeClose">取 消</el-button>
            <el-button
                v-if="mode != 'show'"
                type="primary"
                :loading="isSaveing"
                @click="submit(4)"
            >保 存</el-button>
        </template>
    </full-screen-dialog>
</template>

<script>
import tool from "@/utils/tool";
export default {
    emits: ["success", "closed"],
    data() {
        return {
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            visible: false,
            entityObj: {},
            orgId: 0,
            orgType: 2,
            purchaseUnityProps: {
                label: "purchaseUnityName",
                value: "purchaseUnityId",
            },
            //验证规则
            rules: {
                applyDate: [
                    { required: true, type: "string", message: "请填写" },
                ],
                purchaseUnityId: [
                    { required: true, type: "number", message: "请填写" },
                ],
                applyFile: [
                    { required: true, type: "string", message: "请填写" },
                ],
            },
            props: {
                label: "supplierName",
                value: "supplierId",
            },
            storeProps: {
                label: "storeName",
                value: "storeId",
            },
            materialProps: {
                label: "materialCode",
                value: "materialId",
                items: "materialItems",
            },
            optionsPurchaseUnity: [],
            defaultTime: [
                new Date(2000, 1, 1, 0, 0, 0),
                new Date(2000, 2, 1, 23, 59, 59),
            ],
            fileList: [],
        };
    },
    computed: {},
    methods: {
        async open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        handleBeforeClose() {
            this.$confirm("信息未保存,确定要关闭吗？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.visible = false;
                })
                .catch(() => {
                    // 用户点击了取消按钮，不执行关闭
                });
        },
        updateFileList(newFileList) {
            this.fileList = newFileList;  // 更新 fileList
        },
        //表单提交方法
        submit(status) {
            this.$refs.dialogForm.validate(async (valid) => {
                if (valid) {
                    //下单平台(1:pc端)
                    this.entityObj.sourcePlatformType=1;
                    this.isSaveing = true;
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                "apply-order/import-apply",
                                { ...this.entityObj }
                            );
                        if (returnValue.status == 200) {
                            // if (this.orgType == 1) {
                            //     const formData = new FormData();

                            //     var returnValue1 = await this.$API.auth.PostUpload.post(`apply-order/${returnValue.response}/import-create-apply`,formData);
                            //     if (returnValue1.status == 200) {
                            //     } else {
                            //         this.$message.error(
                            //             "操作失败:" + returnValue.msg
                            //         );
                            //         this.isSaveing = false;
                            //     }
                            // }

                            this.$message.success("操作成功");
                            this.$emit("fatherMethod");
                            this.isSaveing = false;
                            this.visible = false;
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                            this.isSaveing = false;
                        }
                    } catch (error) {
                        this.loading = false;
                        this.isSaveing = false;
                        return false;
                    }
                }
            });
        },
        //表单注入数据
        async setData() {
            this.entityObj = {};
            this.entityObj.applyDate = tool.formatDate(new Date(), 1);
            console.log("applyDate", this.entityObj.applyDate);
        },
        //供货单位
        purchaseUnitySelectProcess(rowData) {
            if (rowData) {
                this.entityObj.purchaseUnityId = rowData.purchaseUnityId;
                this.entityObj.purchaseUnityName = rowData.purchaseUnityName;
                this.entityObj.realName = rowData.realName;
                this.entityObj.mobile = rowData.mobile;
                this.entityObj.address = rowData.address;
                this.entityObj.purchaseUnitys = {
                    purchaseUnityId: rowData.purchaseUnityId,
                    purchaseUnityName: rowData.purchaseUnityName,
                };
            }
        },
        //门店
        storeSelectProcess(rowData) {
            if (rowData) {
                this.entityObj.storeId = rowData.storeId;
                this.entityObj.storeName = rowData.storeName;
                this.entityObj.realName = rowData.realName;
                this.entityObj.mobile = rowData.mobile;
                this.entityObj.address = rowData.address;
                this.entityObj.stores = {
                    storeId: rowData.storeId,
                    storeName: rowData.storeName,
                };
            }
        },
        //文件
        handleFileChange(file, fileList) {
            this.fileList = fileList; // 更新文件列表
            console.log("当前文件:", file);
            console.log("文件列表:", fileList);
        },
    },
    async mounted() {
        this.orgId = tool.data.get("USER_INFO").OrgId;
        this.orgType = tool.data.get("USER_INFO").AccountType;
        console.log(tool.data.get("USER_INFO"));
    },
};
</script>

